package com.zking.web.websocketdemo.controller;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zking.web.websocketdemo.mapper.TestMapper;
import com.zking.web.websocketdemo.model.Student;
import com.zking.web.websocketdemo.model.UserTest;
import com.zking.web.websocketdemo.service.PdfCustomService;
import com.zking.web.websocketdemo.utils.ExcelEasyPoiUtils;
import com.zking.web.websocketdemo.utils.ExcelStyleUtil;
import com.zking.web.websocketdemo.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
public class TestUserController {
    @PostMapping("/import")
    public void importUser(@RequestPart("file") MultipartFile file) throws Exception {
        List<UserTest> users = ExcelUtils.readMultipartFile(file, UserTest.class);
        for (UserTest user : users) {
            System.out.println(user.toString());
        }
    }
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        // 表头数据
        List<Object> head = Arrays.asList("姓名","年龄","性别","头像");
        // 用户1数据
        List<Object> user1 = new ArrayList<>();
        user1.add("诸葛亮");
        user1.add(60);
        user1.add("男");
        user1.add("https://profile.csdnimg.cn/A/7/3/3_sunnyzyq");
        // 用户2数据
        List<Object> user2 = new ArrayList<>();
        user2.add("大乔");
        user2.add(28);
        user2.add("女");
        user2.add("https://profile.csdnimg.cn/6/1/9/0_m0_48717371");
        // 将数据汇总
        List<List<Object>> sheetDataList = new ArrayList<>();
        sheetDataList.add(head);
        sheetDataList.add(user1);
        sheetDataList.add(user2);
        // 导出数据
        ExcelUtils.export(response,"用户表", sheetDataList);
    }
    @Autowired
    private TestMapper testMapper;
    @GetMapping("/test")
    public void Test(){
        testMapper.sel().forEach((key, value) -> {
            System.out.println(key.toString() + " : " + value);
        });
    }

    @PostMapping("/importes")
    public void importUserESel(@RequestPart("file") MultipartFile file) throws Exception {
        ImportParams importParams = new ImportParams();
        //表格标题行数,默认0
        importParams.setTitleRows(0);
        //是否需要校验上传的Excel
        importParams.setNeedVerify(true);
        //告诉easypoi我们自定义的验证器
       // importParams.setVerifyHandler(userVerifyHandler);
        ExcelImportResult<UserTest> result = ExcelImportUtil.importExcelMore(file.getInputStream(),UserTest.class,importParams);
        if (!result.isVerifyFail() && !CollectionUtils.isEmpty(result.getList())) {
            for (UserTest vo : result.getList()) {

                System.out.println(vo);
            }

        }}


    /**
     * 导出Excel文件
     */
    @RequestMapping("/exportes")
    public void ExportExcel(HttpServletResponse response) throws ParseException {
        List<UserTest> userList = new ArrayList<>();
        String input = "UserTest(name=小河, sex=1, age=12, birthday=Mon Jan 02 00:00:00 CST 2012, phone=18975934400, email=1620470381@qq.com)\n" +
                "UserTest(name=小河, sex=2, age=13, birthday=Tue Jan 03 00:00:00 CST 2012, phone=18975934401, email=1620470382@qq.com)\n" +
                "UserTest(name=小河, sex=1, age=14, birthday=Wed Jan 04 00:00:00 CST 2012, phone=18975934402, email=1620470383@qq.com)\n" +
                "UserTest(name=小河, sex=1, age=15, birthday=Thu Jan 05 00:00:00 CST 2012, phone=18975934403, email=1620470384@qq.com)";

        String pattern = "UserTest\\(name=(.*), sex=(\\d), age=(\\d+), birthday=(.*), phone=(.*), email=(.*)\\)";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(input);
        while (m.find()) {
            // 提取每个 UserTest 对象的属性
            String name = m.group(1);
            String sex = m.group(2);
            String age = m.group(3);
            Date birthday = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(m.group(4));
            String phone = m.group(5);
            String email = m.group(6);

            // 创建一个 UserTest 对象并添加到 userList 中
            UserTest user = new UserTest(name, sex, age, birthday, phone, email);
            userList.add(user);
        }
        try {

            ExportParams params = new ExportParams("导出测试员工目录", "测试工作薄");
            params.setTitleHeight((short) 15);
            params.setHeight((short) 12);
            params.setStyle(ExcelStyleUtil.class);
            ExcelEasyPoiUtils.exportExcel(params,userList, UserTest.class,"员工表",response);
           /* //将数据对象导出为工作簿
            Workbook sheets = ExcelExportUtil.exportExcel(new ExportParams("导出测试员工目录", "测试工作薄"), UserTest.class, userList,new MyExcelExportStyler(workbook));

            //设置请求头
            response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode("员工表.xls","UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            sheets.write(outputStream);
            outputStream.close();
            sheets.close();*/
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    @Autowired
    private PdfCustomService pdfCustomService;


    @GetMapping("/admissioncard")
    public void generatorAdmissionCard(HttpServletResponse response){
        try {
            Student student = new Student();
            student.setId(100L);
            student.setName("John Doe");
            student.setSex("男");
            student.setIDNumber("ABC123456789");
            student.setImg("https://pic1.zhimg.com/v2-0b83f86d9d3c38477a62425301a8af35_r.jpg?source=1940ef5c");

            pdfCustomService.generatorAdmissionCard(student, response);
        } catch (UnsupportedEncodingException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }

}
